package com.alibaba.citrus.cr.platform.enhance.repository.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.alibaba.citrus.cr.platform.enhance.repository.ScItemUnitRepository;
import com.alibaba.citrus.cr.platform.enhance.repository.utils.ResultUtils;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.crplatformenhance.domain.scitemunit.dto.CreateRequest;
import com.epoch.app.crplatformenhance.domain.scitemunit.dto.LoadRequest;
import com.epoch.app.crplatformenhance.domain.scitemunit.dto.LoadResponse;
import com.epoch.app.crplatformenhance.domain.scitemunit.dto.ScItemUnitLoadListRequest;
import com.epoch.app.crplatformenhance.domain.scitemunit.dto.UpdateRequest;
import com.epoch.app.crplatformenhance.domain.scitemunit.model.ScItemUnit;
import com.epoch.app.crplatformenhance.domain.scitemunit.service.ScItemUnitService;
import com.epoch.app.crplatformenhance.dto.request.QueryScItemUnitRequest;
import com.epoch.app.crplatformenhance.dto.response.ScItemUnitResponse;
import com.epoch.app.crplatformenhance.enums.CommStatusEnum;
import com.epoch.app.crplatformenhance.sdo.ScItemUnitSDO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 * @author zls
 * @description: TODO
 * @date 2021/9/7 18:08
 */
@Component
public class ScItemUnitRepositoryImpl implements ScItemUnitRepository {
    @Resource
    ScItemUnitService scItemUnitService;

    @Override
    public String createScItemUnit(ScItemUnitSDO scItemUnitSDO) {
        //注意，必输字段是统一规范在这检查还是放在纪元方法配置检查?   待确定
//        checkCreateParams(request);
        //数据模型转换  规范待确定
        CreateRequest createRequest = createRequestConverter(scItemUnitSDO);
        createRequest.setStatus(CommStatusEnum.ENABLE.getCode());
        createRequest.setVersion(1);
        String insertId = ResultUtils.getLastInsertId((Map) scItemUnitService.create(createRequest));
        return insertId;
    }

    private CreateRequest createRequestConverter(ScItemUnitSDO scItemUnitSDO){
        CreateRequest createRequest = new CreateRequest();
        createRequest.setName(scItemUnitSDO.getName());
        createRequest.setCode(scItemUnitSDO.getCode());
        createRequest.setDesc(scItemUnitSDO.getDesc());
        createRequest.setFeatures(scItemUnitSDO.getFeatures());
        return createRequest;
    }

    @Override
    public Boolean editScItemUnit(ScItemUnitSDO scItemUnitSDO) {
        //checkUpdateParams(request);
        UpdateRequest updateRequest = updateRequestConverter(scItemUnitSDO);
        //注意，纪元生成的默认update方法,update条件加上version,,,update数据version+1
        //没有修改会报错...已反馈纪元优化
        Integer count = ResultUtils.getUpdateCount((Map) scItemUnitService.update(updateRequest));
        if(count == 0){
            throw new RuntimeException("数据版本号错误");
        }
        return count > 0;
    }

    private UpdateRequest updateRequestConverter(ScItemUnitSDO scItemUnitSDO){
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.setName(scItemUnitSDO.getName());
        updateRequest.setCode(scItemUnitSDO.getCode());
        updateRequest.setDesc(scItemUnitSDO.getDesc());
        updateRequest.setFeatures(scItemUnitSDO.getFeatures());
        updateRequest.setId(scItemUnitSDO.getId());
        return updateRequest;
    }

    @Override
    public ScItemUnitResponse getScItemUnit(ScItemUnitSDO scItemUnitSDO) {
        LoadRequest loadRequest = new LoadRequest();
        loadRequest.setId(scItemUnitSDO.getId());
        LoadResponse loadResponse = scItemUnitService.load(loadRequest);
        ScItemUnitResponse response = (ScItemUnitResponse) loadResponse.getResult();
        return response;
    }

    @Override
    public PageResult<List<ScItemUnitSDO>> pageQueryScItemUnitList(QueryScItemUnitRequest request) {
        if (request == null) {
            request = new QueryScItemUnitRequest();
        }
        ScItemUnitLoadListRequest loadListRequest = loadListRequestConverter(request);
        Result<List<ScItemUnit>> listResult = scItemUnitService.loadScItemUnitList(loadListRequest);
        List<ScItemUnitSDO> collect = listResult.getResult().stream().map(scItemUnit -> {
            ScItemUnitSDO scItemUnitSDO = new ScItemUnitSDO();
            BeanUtils.copyProperties(scItemUnit, scItemUnitSDO);
            return scItemUnitSDO;
        }).collect(Collectors.toList());
        return PageResult.of(collect, listResult.getTotal());
    }

    private ScItemUnitLoadListRequest loadListRequestConverter(QueryScItemUnitRequest request){
        ScItemUnitLoadListRequest loadListRequest = new ScItemUnitLoadListRequest();
        BeanUtils.copyProperties(request, loadListRequest);
        return loadListRequest;
    }
}
